package com.wanxi.servlet.user;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.dto.ResultDTO;
import com.wanxi.service.UserService;
import com.wanxi.service.impl.UserServiceImpl;
import com.wanxi.util.Tool;
import com.wanxi.vo.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/user/findAll")
public class UserFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的数据
//        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String enable = req.getParameter("enable");
        String start = req.getParameter("start");
        String end = req.getParameter("end");

        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
//        3.将对象封装到VO
        UserVO userVO = UserVO.builder()
//                .id(Tool.strToInt(id))
                .username(Tool.nullToStr(username))
                .sex(Tool.nullToStr(sex))
                .email(Tool.nullToStr(email))
                .phone(Tool.nullToStr(phone))
                .enable(Tool.strToInt(enable))
                .address(Tool.nullToStr(address))
//                .birthday(Tool.nullToDate(birthday))
                .start(Tool.nullToDate(start))
                .end(Tool.nullToDate(end))
                .page(Tool.strToPage(page))
                .limit(Tool.strToLimit(limit))
                .build();
//        4.调用服务逻辑层
        UserService userService = new UserServiceImpl();
//        5.得到返回值
        ResultDTO resultDTO = userService.findAll(userVO);
//        6.返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));


    }
}
